package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.Iterator;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.tools.DragEditPartsTracker;

import edu.pku.sei.modeler.editpolicies.SequenceNodeEditPolicy;
import edu.pku.sei.modeler.figures.ExecutionOccurrenceFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BasicInteractions.ExecutionSpecification;

/**
 * 
 * @author zsj created on 2008-3-23 14:44
 *
 */
public class ExecutionSpecificationEditpart extends AbstractAnchoringNodeEditPart {

	public ExecutionSpecificationEditpart(MetaShapeElement model) {
		super(model);
	}
	
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new SequenceNodeEditPolicy());
	}


	@Override
	protected IFigure createFigure() {
		return new ExecutionOccurrenceFigure();
	}
	
	
	
	

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return null;
//		return super.getDirectEditFigure();
	}


	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		refreshVisuals();
		super.handlePropertyChanged(event);
	}

	@Override
	public DragTracker getDragTracker(Request request) {
		return new DragEditPartsTracker(this) {
			@Override
			protected void updateTargetRequest() {
				super.updateTargetRequest();
				ChangeBoundsRequest request = (ChangeBoundsRequest)getTargetRequest();
				request.getMoveDelta().x = 0;
			}
			
			protected Command getCommand() {
				CompoundCommand command = new CompoundCommand();
				command.setDebugLabel("Drag Object Tracker");
				Iterator iter = getOperationSet().iterator();
				Request request = getTargetRequest();
				request.setType(REQ_MOVE);
				while (iter.hasNext()) {
					EditPart editPart = (EditPart)iter.next();
					if (editPart instanceof ExecutionSpecificationEditpart)
						command.add(editPart.getCommand(request));
				}
				return command;
			}
		};
	}


	@Override
	protected void refreshVisuals() {
		this.getFigure().setOpaque(true);
		super.refreshVisuals();
	}

	protected double calculateAngle(Point location) {
		Rectangle rect = Rectangle.SINGLETON;
		rect.setBounds(getFigure().getBounds());
		getFigure().translateToAbsolute(rect);
		Point ref = rect.getCenter();
		if (location.x >= ref.x) {
			location.x = rect.getRight().x;
		}
		else {
			location.x = rect.getLeft().x;
		}
		double dx = location.x - ref.x;
		double dy = location.y - ref.y;
		return Math.atan2(dy, dx);
	}


}
